package command;


import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import dao.LoginDao;
import db.Connect;
import util.ConfigEnum;
import util.ConfigurationManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: Alexey_Korotayev
 * Date: 9/25/13
 * Time: 5:34 PM
 * To change this template use File | Settings | File Templates.
 */
public class LoginCommand implements Command {
   LoginDao dao=new LoginDao();

   public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
   //     System.out.println("start action");
        String login = request.getParameter("login");
        String password = request.getParameter("password");
        String page;
     if(login!=null && password!=null ){
        if (dao.checkUser(login, password)) {
    //        System.out.println("is check");
            page = ConfigurationManager.getInstance().getProperty(ConfigEnum.MAIN_PAGE_PATH);
           request.setAttribute("user",login);
        } else

        {
            page = ConfigurationManager.getInstance().getProperty(ConfigEnum.ERROR_PAGE_PATH);

        }
     }else     {
         page = ConfigurationManager.getInstance().getProperty(ConfigEnum.LOGIN_PAGE_PATH);
     }
        return page;

    }




}